<?php
namespace app\api\controller;
use app\common\model\Article as aa;  


class Article extends Base
{
    
    public function index()
    {
        $page=request()->param('page');
        $limit=request()->param('limit');
        $key=request()->param('key');
        $db=new aa();

        if(isset($key) && !empty($key)){
        $db = $db->where('title','like','%'.$key.'%');
         
        }


        $info=$db->page($page)->limit($limit)->select();
        $total=$db->count('id');
        return json(['code'=>1,'data'=>$info,'total'=>$total]);
    }

    public function delete(){
        $id=request()->param('id');
        $res=aa::where('id',$id)->delete();
        if ($res) {
            return json(['code'=>1,'data'=>'删除成功']);
        }else{
            return json(['code'=>0,'data'=>'删除失败']);
        }
       
    }
    public function save()
    {
        $data=request()->param();
        $db=new aa();
      

        if (isset($data['id']) && !empty($data['id'])) {
            $res=$db->save($data,['id'=>$data['id']]);
        }else{
            $res=$db->save($data);
        }


        if ($res) {
            return json(['data'=>'更新成功','code'=>1]);
        }else{
            return json(['data' =>'更新失败','code'=>0]);
        }
    }
}
